﻿// =================================================================== 
// 项目说明
//====================================================================
// sushee @Copy Right 2019
// 文件： SmsTaskRepository.cs
// 项目名称：斑马车务
// 创建时间：2019/9/19
// 负责人：sushee
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper.Extensions;
using Dapper;
using YouWei.Infrastructure.Models;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	///短信任务 仓储层
	/// </summary>	
	public class SmsTaskRepository:BaseRepository<SmsTask>
	{		
    
        #region 构造

        public SmsTaskRepository() : base(){}

        public SmsTaskRepository(string connectionString) : base(connectionString){}

        public SmsTaskRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion


        /// <summary>
        /// 根据指定时间获取可用的任务
        /// </summary>
        /// <param name="dtTime">指定时间</param>
        /// <returns></returns>
        public List<SmsTask> GetAllTasks(DateTime dtTime)
        {
            string szSql = @"SELECT 
	                            * 
                            FROM SmsTask t
                            WHERE @dtTime>StartDate and @dtTime<EndDate and t.IsDeleted=0 and t.Status=1";
            var list = Query(szSql, new { dtTime = dtTime });
            return list;

        }

        /// <summary>
        /// 分页查询，带条件
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        public Page<SmsTaskDto> GetPages(int pageIndex, long pageSize, ReqSmsTaskCondition condition)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM SmsTask WITH(NOLOCK) where IsDeleted =0");
            if (!string.IsNullOrWhiteSpace(condition.StartDate))
            {
                sql.Append(" AND CreatedTime>@StartDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.EndDate))
            {
                sql.Append(" AND CreatedTime<@EndDate");
            }
            if (!string.IsNullOrWhiteSpace(condition.TaskName))
            {
                sql.Append(" AND CHARINDEX(@TaskName,TaskName) >0");
            }

            DynamicParameters dparams = new DynamicParameters();
            dparams.Add("@TaskName", condition.TaskName, System.Data.DbType.String);
            dparams.Add("@StartDate", condition.StartDate, System.Data.DbType.String);
            dparams.Add("@EndDate", condition.EndDate, System.Data.DbType.String);
            return dbContext.Connection.Page<SmsTaskDto>(pageIndex, pageSize, sql.ToString(), dparams);
        }

        // public List<string> GetCarSerialsInTask()
        // {
        //     string szSql = @"
        //                         SELECT distinct CarSerial
        //                         FROM [SmsTask]";
        //        return Database.Connection.Query<List<string>>(szSql)
        // }
    }
}